package com.woniuxy.controller;

import com.woniuxy.config.Alipay;
import com.woniuxy.entity.CarOrder;
import com.woniuxy.service.CarOrderService;
import com.woniuxy.service.UserService;
import com.woniuxy.util.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class CarOrderController {
    @Resource
    private CarOrderService carOrderService;
    @Resource
    private UserService userService;
    @RequestMapping("/carOrder/addCarOrder")
    public Result insertCarOrder(CarOrder carOrder) throws Exception{
        Date date = new Date();
        carOrder.setOrderNum(date.getTime()+"");
        carOrder.setOrderTime(date);
        //status为0 表示未支付
        carOrder.setOrderStatus(0);
        //更新用户余额
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userId",carOrder.getUserId());
        map.put("account",carOrder.getOrderDiscount());
        userService.updateUserAccount(map);
        carOrderService.insertCarOrder(carOrder);
        return new Result("200","success",Alipay.getAliPayOrderStr(carOrder),null);
    }
}
